package com.shlh.saas.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shlh.saas.entity.WorkbenchStats;
import com.shlh.saas.mapper.WorkbenchStatsMapper;
import com.shlh.saas.service.WorkbenchStatsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * 工作台统计服务实现类
 */
@Service
public class WorkbenchStatsServiceImpl extends ServiceImpl<WorkbenchStatsMapper, WorkbenchStats> implements WorkbenchStatsService {

    @Override
    public WorkbenchStats getByUserId(Long userId) {
        if (userId == null) {
            throw new IllegalArgumentException("获取工作台统计数据时用户ID不能为空");
        }
        
        WorkbenchStats stats = baseMapper.getByUserId(userId);
        if (stats == null) {
            stats = createUserStats(userId);
        }
        return stats;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkbenchStats createUserStats(Long userId) {
        if (userId == null) {
            throw new IllegalArgumentException("创建工作台统计数据时用户ID不能为空");
        }
        
        WorkbenchStats stats = new WorkbenchStats();
        stats.setUserId(userId);
        stats.setCustomsSearchCount(0);
        stats.setCustomsExportCount(0);
        stats.setEnterpriseSearchCount(0);
        stats.setEnterpriseExportCount(0);
        stats.setCantonBuyerSearchCount(0);
        stats.setCantonBuyerExportCount(0);
        stats.setCantonExhibitorSearchCount(0);
        stats.setCantonExhibitorExportCount(0);
        stats.setLinkedinSearchCount(0);
        stats.setLinkedinExportCount(0);
        stats.setFacebookSearchCount(0);
        stats.setFacebookExportCount(0);
        stats.setTwitterSearchCount(0);
        stats.setTwitterExportCount(0);
        stats.setIntelligentSearchCount(0);
        stats.setEmailReverseCount(0);
        stats.setDomainLookupCount(0);
        stats.setSocialMediaLookupCount(0);
        stats.setCreatedAt(LocalDateTime.now());
        stats.setUpdatedAt(LocalDateTime.now());
        save(stats);
        return stats;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementCustomsSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementCustomsSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementCustomsExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementCustomsExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementEnterpriseSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementEnterpriseSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementEnterpriseExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementEnterpriseExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementCantonBuyerSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementCantonBuyerSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementCantonBuyerExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementCantonBuyerExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementCantonExhibitorSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementCantonExhibitorSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementCantonExhibitorExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementCantonExhibitorExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementLinkedInSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementLinkedInSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementLinkedinSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementLinkedinSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementLinkedInExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementLinkedInExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementLinkedinExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementLinkedinExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementFacebookSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementFacebookSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementFacebookExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementFacebookExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementTwitterSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementTwitterSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementTwitterExportCount(Long userId, Integer count) {
        ensureStatsExists(userId);
        baseMapper.incrementTwitterExportCount(userId, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementIntelligentSearchCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementIntelligentSearchCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementEmailReverseCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementEmailReverseCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementDomainLookupCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementDomainLookupCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementSocialMediaLookupCount(Long userId) {
        ensureStatsExists(userId);
        baseMapper.incrementSocialMediaLookupCount(userId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementTwitterViewCount(Long userId) {
        ensureStatsExists(userId);
        // 如果需要在数据库中添加新的字段和方法，可以使用通用更新语句
        // 这里假设我们可以使用其他已有的方法，比如搜索次数方法来统计查看次数
        // 实际使用时，应该在数据库中添加相应的字段和mapper方法
        baseMapper.incrementTwitterSearchCount(userId);
    }
    
    /**
     * 确保用户的统计数据存在
     * @param userId 用户ID
     */
    private void ensureStatsExists(Long userId) {
        if (userId == null) {
            throw new IllegalArgumentException("检查工作台统计数据时用户ID不能为空");
        }
        
        WorkbenchStats stats = baseMapper.getByUserId(userId);
        if (stats == null) {
            createUserStats(userId);
        }
    }
}